﻿using HBase;
using HBase.db;
using hCommand;
using Net.http;
using Persistence;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text.RegularExpressions;

namespace WebApi.Controller {
    [RoutePrefix("pda")]
    public class PdaController : BaseApiController {


        ICoreStockServer coreStockServer = WMSDalFactray.getDal<ICoreStockServer>();
        ICSDServer csdServer = WMSDalFactray.getDal<ICSDServer>();
        IDooServer dooServer = WMSDalFactray.getDal<IDooServer>();
        IDoPkServer doPkServer = WMSDalFactray.getDal<IDoPkServer>();
        //原料分拣出库界面显示
        [HttpGet, Route("getStock/{boxCode}")]
        public BaseResult getStock(String boxCode) {
            CoreStock list = coreStockServer.getCoreStockByCode(boxCode);
            if (list == null) {
                return BaseResult.Error("托盘条码不存在");
            }
            StockDto dto = new StockDto();
            dto.main = list;
            dto.detList = csdServer.getDetail(dto.main.id, true).OrderBy(x => x.status).ToList();
             
            return new BaseResult(dto);
        }
        [HttpPost, Route("csds/{dooNo}"), ControlName("选择明显")]
        public BaseResult csds(  String dooNo) {
            WhDoo doo = dooServer.getDoo(dooNo);
            if (doo == null) {
                return BaseResult.Ok(new List<CoreStockDet>());
            }
            List<CoreStockDet> list = dooServer.getCsdList(doo);
            return BaseResult.Ok(list); 
        }

        [HttpPost, Route("getPKCSD/{pkNo}"), ControlName("盘库明显")]
        public BaseResult getPKCSD(String pkNo) {
            List < CoreStockDet > csds = doPkServer.getCSDList(pkNo);
             
            return BaseResult.Ok(csds);
        }


        [HttpGet, Route("getItem/{name}")]
        public BaseResult getitem(String name) {
            List<CoreItem> list = MyCache.getList<CoreItem>();
            List<CoreItem> res = new List<CoreItem>();
            foreach (CoreItem ci in list) {
                if ((ci.code + ci.name + ci.gg).IndexOf(name) != -1) {
                    res.Add(ci);
                    if (res.Count == 10) {
                        break;

                    }
                }

            }


            return new BaseResult(res);
        }
        [HttpGet, Route("getUnits")]
        public BaseResult getUnits() {
            List<DictRes> dicts = MyCache.getList<DictRes>();
            List<DictRes> res = new List<DictRes>();
            foreach (DictRes dict in dicts) {
                if (dict.dType == "units" && dict.type == 1) {
                    res.Add(dict);
                }


            }

            return new BaseResult(res);
        }

 

        //托盘出库
        [HttpPost, Route("outStockByBoxCode"), ControlName("托盘出库")]
        public BaseResult outStockByBoxCode([FromBody] stockInfoDto dto) {
            string[] sArray = Regex.Split(dto.boxCode, ",", RegexOptions.IgnoreCase);
            for (int i = 0; i < sArray.Length - 1; i++) {
                CoreStock coreStock = null;
                if (coreStock == null) {
                    return BaseResult.Error("托盘条码不存在");
                }
                //
                var coreStockDetailList = coreStockServer.GetAll<CoreStockDet>("and pId=@stockId", new { stockId = coreStock.id });
                if ( coreStockDetailList.Count != 0) {
                    return BaseResult.Error("出库明细不存在");
                }
                coreStockServer.UpdateStatusByStockId(coreStock.id, StockStatus.PDA出库删除);
                
            }
            return BaseResult.Ok("ok");
        }

     
 
        [HttpGet, Route("bandDetPingku/{id}/{pid}"), ControlName("平库入库")]
        public BaseResult bandDetPingku(long id, long pid) {

            TransRes r = WMSTransactionFacade.doCsd2CsEngine(id, pid,true);
            return new BaseResult(r.msg); 
        }

        [HttpGet, Route("pinkuIn/{boxCode}/{des}/{type}"), ControlName("更改位置")]
        public BaseResult pinkuIn(string boxCode, int des, int type) {

            CoreStock cs = coreStockServer.getCoreStockByCode(boxCode);
            if (cs == null) {
                return new BaseResult("托盘不存在");
            }
            WMSTransactionFacade.doCsdCsMoveEngine(cs.id, des); 
            return new BaseResult("成功");
          
        }

    }
    public class stockInfoDto {
        public long detailId;
        public int countInput;
        public string boxCode;
        public int isCouyan;
        public int isZhentuo;
        public string barCode;
    }

}
